package gui.customizer.panes.statistics;

import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import core.models.IfaceStat;

import net.SSHConnection;
import net.SSHConnectionProxy;
import net.miginfocom.swing.MigLayout;

public class Interfaces extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -694220991804136757L;

	/**
	 * Create the panel.
	 * 
	 * @throws IOException
	 */
	public Interfaces() throws IOException {
		setLayout(new MigLayout("", "[grow]15[grow]", "[grow]"));
		
		if (!SSHConnectionProxy.isConnected())
			return;
		SSHConnection conn = new SSHConnectionProxy();

		// List of eth* interfaces
		ArrayList<String> ethInterfaces = new ArrayList<String>();
		List<String> interfaces = conn.getInterfacesNames();
		for (String s : interfaces) {
			if (s.matches("eth\\d+"))
				ethInterfaces.add(s);
		}

		ArrayList<IfaceStat> stats = new ArrayList<IfaceStat>(
				ethInterfaces.size());
		for (String s : ethInterfaces) {
			IfaceStat stat = new IfaceStat(s);
			stat.populateData();
			stats.add(stat);
		}

		final CategoryDataset bytesDataset = createBytesDataset(stats);
		final CategoryDataset packetsDataset = createPacketsDataset(stats);

		final JFreeChart bytesChart = createBytesChart(bytesDataset);
		final ChartPanel bytesPanel = new ChartPanel(bytesChart);
		
		add(bytesPanel, "cell 0 0,grow");
		
		final JFreeChart packetsChart = createPacketsChart(packetsDataset);
		final ChartPanel packetsPanel = new ChartPanel(packetsChart);
		add(packetsPanel, "cell 1 0,grow");
	}

	private JFreeChart createPacketsChart(CategoryDataset dataset) {
		final JFreeChart chart = ChartFactory.createBarChart("Packets",
				"Interfaces", "Number of Packets", dataset, PlotOrientation.VERTICAL, true,
				true, false);
		
		chart.setBorderPaint(Color.WHITE);
		
		CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.white);
        
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        
        final CategoryItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesItemLabelsVisible(0, Boolean.TRUE);
        
        final BarRenderer br = (BarRenderer) plot.getRenderer();
        br.setItemMargin(0.1);
        
        final CategoryAxis domainAxis = plot.getDomainAxis(); 
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

		return chart;
	}

	private JFreeChart createBytesChart(CategoryDataset dataset) {
		final JFreeChart chart = ChartFactory.createBarChart("Data",
				"Interfaces", "MB", dataset, PlotOrientation.VERTICAL, true,
				true, false);
		
		chart.setBorderPaint(Color.WHITE);
		
		CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.white);
        
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
        
        final CategoryItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesItemLabelsVisible(0, Boolean.TRUE);
        
        final BarRenderer br = (BarRenderer) plot.getRenderer();
        br.setItemMargin(0.1);
        
        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

		return chart;
	}

	private CategoryDataset createBytesDataset(List<IfaceStat> stats) {
		final String sSent = "Transmitted";
		final String sReceived = "Received";
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		for (IfaceStat stat : stats) {
			dataset.addValue(stat.getMegaBytesSent(), sSent, stat.getName());
			dataset.addValue(stat.getMegaBytesReceived(), sReceived, stat.getName());
		}

		return dataset;
	}

	private CategoryDataset createPacketsDataset(List<IfaceStat> stats) {
		final String sSent = "Transmitted";
		final String sReceived = "Received";
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		for (IfaceStat stat : stats) {
			dataset.addValue(stat.getPacketsSent(), sSent, stat.getName());
			dataset.addValue(stat.getPacketsReceived(), sReceived,
					stat.getName());
		}

		return dataset;
	}

}
